package com.feiyang.ni.test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class MyFrame extends JFrame implements ActionListener {
    //创建两个按钮
    JButton btn1 = new JButton("点我啊~");
    JButton btn2 = new JButton("点我啊~");
    //创建一个窗口
    public MyFrame(){
        //设置窗口的大小
        this.setSize(603,680);
        //设置窗口的标题
        this.setTitle("检查测试");
        //设置窗口置顶
        this.setAlwaysOnTop(true);
        //设置窗口居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //关闭默认窗口
        this.setLayout(null);
        //显示（建议放在最后）
        this.setVisible(true);

        //对按钮btn（组件）设置尺寸定位
        btn1.setBounds(0,0,100,50);
        btn2.setBounds(100,0,100,50);
        //对组件设置监听
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        //将组件添加到窗口中
        this.getContentPane().add(btn1);
        this.getContentPane().add(btn2);
    }


    //对组件设置监听
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取被按的按钮对象
        Object sourse = e.getSource();
        if (sourse == btn1) {
            btn1.setSize(200,200);
        } else if (sourse == btn2) {
            Random r = new Random();
            btn2.setLocation(r.nextInt(500),r.nextInt(500));
        }
    }
}

